-- RODA DEPOIS NO MEMENTO DA INCLUSAO E EXCLUSAO DO CONSUMO PARA ATUALIZAR AS TABELAS AMARRADAS
CREATE OR REPLACE TRIGGER T_ATS_CONSUMO
BEFORE INSERT OR DELETE ON ATS_CONSUMO FOR EACH ROW
DECLARE
v_consumo NUMBER;
v_erro NUMBER(1);
BEGIN
  IF INSERTING THEN
    SELECT SEQ_ATS_CONSUMO.NEXTVAL INTO v_consumo FROM DUAL;
    :NEW.CON_CODIGO := v_consumo;
    
     UPDATE ATS_DETOFCONS_ETIQ SET OFE_QTDCON = OFE_QTDCON + :NEW.CON_QTDCON
      WHERE  OFE_CODETI = :NEW.CON_CODETI;
      
         
  ELSE 
    v_erro := 0;
    UPDATE ATS_DETOFCONS_ETIQ SET OFE_QTDCON = OFE_QTDCON - :NEW.CON_QTDCON
    WHERE  OFE_CODETI = :NEW.CON_CODETI;
  END IF;           
END;